package com.example.demo.three;

import com.example.demo.util.RabbitMQUtils;
import com.rabbitmq.client.Channel;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author chensixiang chensixiang1234@gmail.com
 */
public class Work02 {
    public static final String QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtils.getChannel();
        // 设置不公平分发
        // channel.basicQos(1);
        // 欲取值
        channel.basicQos(2);
        System.out.println("C1等待接收消息......时间短");

        channel.basicConsume(QUEUE_NAME, false,
                (consumerTag, message) -> {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 不批量应答信道中的消息
                    System.out.println("接收到的消息 -> " + new String(message.getBody()));
                    channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
                }, consumerTag -> {
                    System.out.println(consumerTag + " -> 消息被取消");
                });

    }
}
